package com.foreverwin.mes.warehouse.service.impl;

import com.foreverwin.mes.common.enums.HandleEnum;
import com.foreverwin.mes.core.exception.BusinessException;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.meapi.model.CustomFields;
import com.foreverwin.mes.meapi.model.Item;
import com.foreverwin.mes.meapi.model.Vendor;
import com.foreverwin.mes.meapi.service.CustomFieldsService;
import com.foreverwin.mes.meapi.service.ItemService;
import com.foreverwin.mes.meapi.service.VendorService;
import com.foreverwin.mes.warehouse.constant.Constants;
import com.foreverwin.mes.warehouse.dto.InventoryReceiveDto;
import com.foreverwin.mes.warehouse.service.InventoryReceiveService;
import com.foreverwin.mes.warehouse.strategy.InventoryReceiveStrategy;
import com.foreverwin.mes.warehouse.strategy.InventoryReceiveStrategyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 1.根据ERP接口同时传输过来的包装规格系统自动进行拆分
 * 2.拆分之后直接打印库存标贴
 * 3.部分原料不需要拆分
 * 4.MES接收到物料后置为不可用状态
 *
 * @author Ervin Chen
 * @date 2020/2/10 12:49
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class InventoryReceiveServiceImpl implements InventoryReceiveService {

    @Autowired
    private CustomFieldsService customFieldsService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private VendorService vendorService;

    @Override
    public boolean receive(List<InventoryReceiveDto> inventoryReceiveDtos) {

        inventoryReceiveDtos.forEach(inventoryReceiveDto -> {
            String site = inventoryReceiveDto.getSite();
            String item = inventoryReceiveDto.getItem();

            Item itemObj = itemService.selectItemByCode(site, item);
            if (itemObj == null) {
                throw BusinessException.build("common.item.itemNotExist", item);
            }

            String itemBo = itemObj.getHandle();
            inventoryReceiveDto.setItemBo(itemBo);
            String splitBatchMode = getItemSplitBatchMode(itemBo);
            InventoryReceiveStrategy inventoryReceiveStrategy = InventoryReceiveStrategyFactory.getInventoryReceiveStrategy(splitBatchMode);
            inventoryReceiveStrategy.validate(inventoryReceiveDto);
        });

        inventoryReceiveDtos.forEach(inventoryReceiveDto -> {
            String site = inventoryReceiveDto.getSite();
            String item = inventoryReceiveDto.getItem();

            createVendor(inventoryReceiveDto);

            Item itemObj = itemService.selectItemByCode(site, item);
            String itemBo = itemObj.getHandle();
            String splitBatchMode = getItemSplitBatchMode(itemBo);
            InventoryReceiveStrategy inventoryReceiveStrategy = InventoryReceiveStrategyFactory.getInventoryReceiveStrategy(splitBatchMode);
            inventoryReceiveStrategy.initService(site);
            inventoryReceiveStrategy.receive(inventoryReceiveDto);
        });
        return false;
    }

    public String getItemSplitBatchMode(String itemBo) {
        CustomFields customFields = new CustomFields();
        customFields.setHandle(itemBo);
        customFields.setAttribute(Constants.SPLIT_BATCH);
        List<CustomFields> customFieldsList = customFieldsService.selectList(customFields);
        if (customFieldsList.isEmpty()) {
            throw BusinessException.build("common.item.splitBatchNotExist", HandleEnum.ITEM.getPart(itemBo, 1));
        }
        return customFieldsList.get(0).getValue();
    }

    public void createVendor(InventoryReceiveDto inventoryReceiveDto) {
        String site = inventoryReceiveDto.getSite();
        String vendorId = inventoryReceiveDto.getVendorId();
        String vendor = inventoryReceiveDto.getVendor();
        if (!StringUtils.isBlank(vendorId)) {
            String hanedle = HandleEnum.VENDOR.getHandle(site, vendorId);
            Vendor vendorDo = vendorService.selectById(hanedle);
            if (vendorDo == null) {
                Date now = new Date();
                vendorDo = new Vendor();
                vendorDo.setHandle(hanedle);
                vendorDo.setSite(site);
                vendorDo.setVendorCode(vendorId);
                vendorDo.setVendorName(StringUtils.isBlank(vendor) ? vendorId : vendor);
                vendorDo.setComponentCanBeConsumed("true");
                vendorDo.setComponentCanBeReceived("true");
                vendorDo.setModifiedDateTime(now);
                vendorDo.setCreatedDateTime(now);
                vendorService.insert(vendorDo);
            }
        }
    }
}
